
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Text;
using Microsoft.Phone.Shell;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using System.Windows.Threading;


namespace sdkSocketsCS
{
    public partial class MainPage : basepage
    {
        Motion motion; 
        DispatcherTimer timer;       

        
        Double[] attitude=new double[3];
        Vector3 acceleration;
        bool canStart = false;
        String default_IP = "192.168.1.10";
        int default_Port = 13001;

 

        // Constructor
        public MainPage()
        {
            InitializeComponent();            

            textBlock_IP.Text = default_IP;

            textBlock_Port.Text = default_Port.ToString();

            // Initialize the timer and add Tick event handler, but don't start it yet.
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(30);
            timer.Tick += new EventHandler(timer_Tick);
                      
            motion = new Motion();

            // Specify the desired time between updates. The sensor accepts
            // intervals in multiples of 1s.            
            motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(30);
           

            motion.CurrentValueChanged += new
            EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
           
            motion.Start();
            timer.Start();
            

        }

        #region Update sensor data

        void motion_CurrentValueChanged(object sender,
        SensorReadingEventArgs<MotionReading> e)
        {
            if (motion.IsDataValid)
            {
                attitude[0] = MathHelper.ToDegrees(e.SensorReading.Attitude.Yaw);
                attitude[1] = MathHelper.ToDegrees(e.SensorReading.Attitude.Pitch);
                attitude[2] = MathHelper.ToDegrees(e.SensorReading.Attitude.Roll);
                acceleration = e.SensorReading.DeviceAcceleration;
            }
        }

        #endregion

        #region Send sensor data periodicly

        void timer_Tick(object sender, EventArgs e)
        {            

            if ((motion.IsDataValid)&&(canStart==true))
            {
                AsynchronousClient client = new AsynchronousClient(this.ServerName, this.PortNumber);
                String stringToSend = attitude[0].ToString("0.00") + "|"
                    + attitude[1].ToString("0.00") + "|" + attitude[2].ToString("0.00")
                    + "|"+ acceleration.X.ToString("0.00")
                    + "|" + acceleration.Y.ToString("0.00")
                    + "|" + acceleration.Z.ToString("0.00");

                client.SendData(stringToSend);
            }           

        }

        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            #region initialize server name and port number

            if (String.IsNullOrWhiteSpace(txtServerName.Text))
            {
                //default IP
                (Application.Current as App).HostName = default_IP;
            }
            else
            {
                (Application.Current as App).HostName = txtServerName.Text;
            }

            if (String.IsNullOrWhiteSpace(txtPortNumber.Text))
            {
                //default Port Number
                (Application.Current as App).PortNumber = default_Port;
            }
            else
            {
                (Application.Current as App).PortNumber = Convert.ToInt32(txtPortNumber.Text);
            }

            #endregion

            #region use the button to control start and pause

            //let the connection start
            if (canStart == false)
            {
                canStart = true;
                button1.Content = "Pause";
            }
            else
            {
                canStart = false;
                button1.Content = "Start";
            }

            #endregion

        }   

   

    }
}
